package com.bcx.crm.java.proxy.jdkProxy2;

import java.lang.reflect.*;

/**
 * Created by ys on 2018/1/26.
 */
public class HelloProxy {

    public interface IHello{
        String getHelloValue(String value);
    }
    static class Hello implements IHello{

        @Override
        public String getHelloValue(String value) {
            System.out.println("打招呼说:"+value);
            return "实现了Hello返回值是:"+value;
        }
    }
    static class MyInvocationHandler implements InvocationHandler{
        private Object target;
        public MyInvocationHandler(Object target){
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("执行之前");
            Object res = method.invoke(target, args);
            System.out.println("执行之后");
            return res;
        }
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class proxyClass = Proxy.getProxyClass(IHello.class.getClassLoader(), IHello.class);
        Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
        IHello iHello =(IHello) constructor.newInstance(new MyInvocationHandler(new Hello()));
        System.out.println(iHello.getHelloValue("123"));
    }

}
